package com.wngbms.multidatasource.controller;

//import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class TestController {

//    @NacosValue(value = "${nacosValue:default}", autoRefreshed = true)
    private String nacosValue;


    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/nacosValue")
    public String v() {
        return nacosValue;
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/async")
    public String async() {
        log.info("==>cxt:{}", applicationContext);
        TestController testController = applicationContext.getBean(TestController.class);
        log.info("==>controller:{}", testController);

        for (int i = 0; i < 100; i++) {
            testController.m();
        }

        log.info("==>end");
        return "success";
    }

    @Async
    public void m() {
        try {
            log.info("===>start");
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
